﻿using System.Windows;
using System.Windows.Input;
using InseSoft.Infraestructure;

namespace InseSoft.ViewModels
{
    public class MainViewModel : ObservableObject
    {
        #region Declarations

        private readonly ApplicationSession session;

        #endregion

        #region Constructor

        public MainViewModel(ApplicationSession session, LoginViewModel loginViewModel, IWindowLauncher windowLauncher)
        {
            this.session = session;
            LoginViewModel = loginViewModel;
            WindowLauncher = windowLauncher;
        }
        #endregion

        #region Properties

        public LoginViewModel LoginViewModel { get; set; }

        public IWindowLauncher WindowLauncher { get; set; }

        #endregion

        #region Commands

        public ICommand EmpresasCommand
        {
            get { return new RelayCommand(EmpresaExecute, EmpresaCanExecute); }
        }

        public ICommand ClientesCommand
        {
            get { return new RelayCommand(ClienteExecute, ClienteCanExecute); }
        }

        public ICommand LineasCommand
        {
            get { return new RelayCommand(LineaExecute, LineaCanExecute); }
        }

        public ICommand MaterialesCommand
        {
            get { return new RelayCommand(MaterialesExecute, MaterialCanExecute); }
        }

        public ICommand TipoCambioCommand
        {
            get { return new RelayCommand(TipoCambioExecute, TipoCambioCanExecute); }
        }

        public ICommand CotizacionCommand
        {
            get { return new RelayCommand(CotizacionExecute, CotizacionCanExecute); }
        }

        public ICommand ExitCommand
        {
            get { return new RelayCommand(ExitExecute); }
        }

        public ICommand LoginCommand
        {
            get { return new RelayCommand(LoginExecute, () => !LoginViewModel.IsNotLoggedIn); }
        }

        #endregion

        #region Methods

        private bool EmpresaCanExecute()
        {
            return session.Usuario != null && session.Usuario.CanAccessMenu("Empresas");
        }

        private bool ClienteCanExecute()
        {
            return session.Usuario != null && session.Usuario.CanAccessMenu("Clientes");
        }

        private bool LineaCanExecute()
        {
            return session.Usuario != null && session.Usuario.CanAccessMenu("Líneas");
        }

        private bool MaterialCanExecute()
        {
            return session.Usuario != null && session.Usuario.CanAccessMenu("Materiales");
        }

        private bool TipoCambioCanExecute()
        {
            return session.Usuario != null && session.Usuario.CanAccessMenu("Tipo de cambio");
        }

        private bool CotizacionCanExecute()
        {
            return session.Usuario != null && session.Usuario.CanAccessMenu("Cotizar");
        }

        private void EmpresaExecute()
        {
            WindowLauncher.Show(AppView.Empresas);
        }

        private void ClienteExecute()
        {
            WindowLauncher.Show(AppView.Clientes);
        }

        private void LineaExecute()
        {
            WindowLauncher.Show(AppView.Lineas);
        }

        private void MaterialesExecute()
        {
            WindowLauncher.Show(AppView.Materiales);
        }

        private void TipoCambioExecute()
        {
            WindowLauncher.Show(AppView.TipoCambio);
        }

        private void CotizacionExecute()
        {
            WindowLauncher.Show(AppView.Cotizacion);
        }

        private void ExitExecute()
        {
            Application.Current.Shutdown();
        }

        private void LoginExecute()
        {
            LoginViewModel.IsNotLoggedIn = true;
        }
        
        #endregion
    }
}
